<template>
  <div class="goods-list" ref="viewBox">
    <my-header titleName="商品列表"></my-header>
    <ul>
      <!-- 1. 利用v-for渲染商品列表页面 通过id跳转到商品详情页面-->
      <router-link class="goods-li" :to="`/goodsdetail/${item.id}`" v-for="(item,index) in goodsList" :key="index">
        <div class="goods-container">
          <img :src="item.img_url" :alt="item.title">
          <h4>{{item.title}}</h4>
          <div class="goods-info">
            <div class="price-info">
              <span class="price-new">￥{{item.sell_price}}</span>
              <span class="price-old">￥{{item.market_price}}</span>
            </div>
            <div class="sell-info">
              <span class="sell-status">热卖中</span>
              <span class="sell-leftcount">剩余{{item.stock_quantity}}件</span>
            </div>
          </div>
        </div>
      </router-link>
    </ul>
  </div>
</template>
<script>
import { getGoodsList } from '@/api'
import myHeader from '@/components/myHeader'
export default {
  components: {
    myHeader
  },
  data () {
    return {
      // 自定义一个页面数
      pageIndex: 1,
      // 存放商品列表的数组
      goodsList: [],
      imgLength: 0
    }
  },
  created () {
    // 1.2 调用商品列表初始化方法
    this.initGoods()
  },
  mounted () {

  },
  methods: {
    // 1.1 商品列表初始化方法
    initGoods () {
      getGoodsList(this.pageIndex).then(res => {
        console.log(res)
        let oldGoodsList = this.goodsList
        this.goodsList = [...oldGoodsList, ...res.message]
        // this.goodsList = res.message

        this.imgLength += res.message.length
        console.log(res.message.length)
        if (!(res.message.length < 10)) {
          this.lazyLoad()
        } else {
          // 将页面滚动事件移除
          window.removeEventListener('scroll', this.fn, false)
        }
      })
    },
    // 懒加载
    lazyLoad () {
      this.$nextTick(() => {
        window.addEventListener('scroll', this.fn, false)
      })
    },
    // 懒加载处理函数
    fn () {
      let _this = this
      let imgArr = document.querySelectorAll('img')
      let len = this.imgLength
      let seeHeight = document.documentElement.clientHeight
      let scrollTop = document.body.scrollTop || document.documentElement.scrollTop
      // 最后一张图片的top偏移量 > 屏幕的高度 + 页面滚动的top偏移量 --> 证明页面已经滚到该位置
      if (imgArr[len - 1].offsetTop < seeHeight + scrollTop) {
        console.log('到底了')
        // 判断到底了，发请求
        _this.pageIndex++
        _this.initGoods()
        console.log(this)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.goods-list {
  margin: 40px 0 0 0;
  background-color: #f4f4f4;
  ul {
    display: flex;
    flex-wrap: wrap;
    font-size: 10px;
  }
  h4 {
    font-size: 12px;
    font-weight: bold;
    height: 32px;
    // 多行文本超出省略号
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .goods-li {
    width: 50%;
    box-sizing: border-box;
    padding-top: 5px;
    display: flex;
    justify-content: column;
    text-decoration: none;
    .goods-container {
      width: 100%;
      background-color: #fff;
      padding: 5px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .price-info {
      display: flex;
      justify-content: space-between;
      .price-new {
        color: red;
      }
      .price-old {
        text-decoration: line-through;
      }
    }
    .sell-info {
      display: flex;
      justify-content: space-between;
    }
  }

  .goods-li:nth-child(2n+1) {
    padding-right: 5px;
  }
  .goods-li:nth-child(2n) {
    padding-left: 5px;
  }
}
</style>
